home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_166 / autograf / autograf.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  11KB  |  502 lines

  1. /*
  2.      autograf.c : Program to graph various data from the auto datafile
  3.  
  4.      By Joel Swank September 3, 1988
  5.      Version 1.0
  6.  
  7.      */
  8. /*
  9.    Reads a datafile of gasoline purchases of the following format:
  10.    Individual fields separated by commas.
  11.    * date - character format
  12.    * odometer reading - 999999.9
  13.    * price per gallon - $9.999
  14.    * Total cost of purchase - $9999.99
  15.    * gallons - 999.9
  16.    * place and brand of gas - character
  17.  
  18.    Execute from CLI or WorkBench.
  19.  
  20. */
  21.  
  22. #include <exec/types.h>
  23. #include <exec/memory.h>
  24. #include <exec/tasks.h>
  25. #include <intuition/intuition.h>
  26. #include <intuition/intuitionbase.h> 
  27. #include <libraries/dosextens.h> 
  28. #include <graphics/gfxbase.h> 
  29. #include <graphics/gfx.h> 
  30. #include <graphics/gfxmacros.h> 
  31. #include <graphics/display.h> 
  32. #include <graphics/text.h> 
  33. #include <devices/printer.h> 
  34. #include <stdio.h>
  35.  
  36. #include "fileio.h"
  37. #include "wtext.h"
  38. #include "menu.h"
  39.  
  40. #ifdef NULL
  41. #undef NULL
  42. #endif
  43. #define NULL ((void *)0)
  44.  
  45. #define INTUITION_REV 1L
  46. #define STDIN 0
  47. #define MAXYR 99
  48. #define MINYR 0
  49.  
  50. /* 
  51.  *  Data describing the Window
  52.  */
  53.  
  54. static struct NewWindow    New_Window = {
  55.     0, 0,            /* Take all of the */
  56.     640, 200,    /* standard screen       */
  57.     -1, -1,            /* Default pens */
  58.     MOUSEMOVE+MENUPICK+GADGETUP+CLOSEWINDOW,    /* Inputs acceppeted */
  59.     WINDOWCLOSE        /* Fairly standard window */
  60.     | WINDOWDEPTH | WINDOWDRAG | SMART_REFRESH ,
  61.     NULL,            /* gadgets */
  62.     (struct Image *) NULL,
  63.     (UBYTE *) NULL ,    /* title */
  64.     (struct Screen *) NULL,        /* filled at startup */
  65.     (struct BitMap *) NULL,
  66.     0, 0, 0, 0,        /* no change sizes, doesn't matter */
  67.     WBENCHSCREEN
  68.     } ;
  69.  
  70.  
  71.  
  72. /*
  73.  *    Data describing the requestor text
  74.  *
  75.  */
  76.  
  77. struct IntuiText oktxt = {
  78.     2,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  79.     5,3,    /* XY origin relative to container TopLeft */
  80.     NULL,    /* font pointer or NULL for default */
  81.     (UBYTE *)"OK",    /* pointer to text */
  82.     NULL    /* next IntuiText structure */
  83. };
  84.  
  85. struct IntuiText nodatamsg = {
  86.     0,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  87.     14,16,    /* XY origin relative to container TopLeft */
  88.     NULL,    /* font pointer or NULL for default */
  89.     (UBYTE *)"No data selected - check options",    /* pointer to text */
  90.     NULL    /* next IntuiText structure */
  91. };
  92.  
  93. struct IntuiText prfailtxt = {
  94.     2,1,JAM2,    /* front and back text pens, drawmode and fill byte */
  95.     14,16,    /* XY origin relative to container TopLeft */
  96.     NULL,    /* font pointer or NULL for default */
  97.     (UBYTE *)"Printer failed to open",    /* pointer to text */
  98.     NULL    /* next IntuiText structure */
  99. };
  100.  
  101. /*
  102.  * Some things that need to be shared with done.
  103.  */
  104. extern struct IntuitionBase    *IntuitionBase ;
  105. struct Window    *Wind = NULL ;
  106. static struct MsgPort        *CreatePort() ;
  107. extern struct DosLibrary *DosBase ;
  108. extern struct GfxBase *GfxBase ;
  109. struct RastPort *rp;
  110. struct Library *DiskfontBase = NULL;
  111. struct IntuiMessage    *msg;
  112. struct FileIOSupport *FIOSupp = NULL;
  113.  
  114.  
  115. /* data to be shared */
  116.  
  117. /* flags for which grafs to display */
  118. int cpm = TRUE;
  119. int ppg = TRUE;
  120. int mpg = TRUE;
  121.  
  122. int syr = -1;        /* for requested start year */
  123. int eyr = -1;        /* for requested end year */
  124.  
  125. char tempbuf[80];
  126. extern float rawcost[1000];    /* save cost data of each entry */
  127. extern float pricesave[1000];    /* save price data of each entry */
  128. extern int yrsave[1000];        /* save year of each entry */
  129. extern float odsave[1000];        /* save odometer data of each entry */
  130.  
  131. int numrecs;        /* number of records in data space */
  132. double atof();
  133. char *names[10];
  134. char *malloc();
  135. /* some window and screen titles */
  136. char autotitle[] = "AutoGraf - Select from Menu";
  137. char screentitle[] = "AutoGraf Version 1.0 080488";
  138.  
  139.  
  140. main(argc,argv)
  141. int argc;
  142. char *argv[];
  143. {
  144.  
  145. int cmd, i;
  146. int cnt;
  147. long menunum;
  148.  
  149.  
  150.  
  151. /*************************************************
  152.      OPEN everything
  153. *************************************************/
  154.  
  155.  
  156. if ((IntuitionBase = (struct IntuitionBase *)
  157.     OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  158.     done(21);
  159.  
  160. GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", NULL);
  161. if (GfxBase == NULL) done(22);
  162. if ((DosBase = (struct DosLibrary *)OpenLibrary("dos.library", 0)) == NULL)
  163.     done(27);
  164.  
  165. if ((Wind = (struct Window *) OpenWindow(&New_Window)) == NULL)
  166.     done(25);
  167. rp = Wind->RPort;
  168. if (NULL == (FIOSupp = GetFileIOSupport()))
  169.     done(26);
  170.  
  171.  
  172. /*************************************************
  173.      Main processing loop 
  174. *************************************************/
  175.  
  176.  
  177.     do_intro();
  178.     SetWindowTitles(Wind,(UBYTE *) autotitle,
  179.             (UBYTE *) screentitle);
  180.     SetMenuStrip(Wind,&MenuList1);
  181.     while (1)
  182.         {
  183.         Wait( 1L << Wind->UserPort->mp_SigBit);    /* wait on mesg */
  184.         while(msg = (struct IntuiMessage *) GetMsg(Wind->UserPort)) 
  185.             {
  186.             switch(msg->Class)
  187.                 {
  188.                 case CLOSEWINDOW:
  189.                     ReplyMsg(msg);
  190.                     done(0);
  191.                 case GADGETUP:
  192.                     ReplyMsg(msg);
  193.                     continue;
  194.                 case MENUPICK:
  195.                     menunum = msg->Code;
  196.                     ReplyMsg(msg);
  197.                     if (menunum != MENUNULL) do_pick(menunum);
  198.                     SetWindowTitles(Wind,(UBYTE *)
  199.                         autotitle, -1L);
  200.                     continue;
  201.                 }
  202.             ReplyMsg(msg);
  203.             }
  204.         }
  205.     done(0);
  206. }
  207.  
  208.  
  209. /*
  210.  * do_pick : handle chain of menu selections
  211.  */
  212.  
  213. do_pick(menunum)
  214. long menunum;
  215. {
  216. struct MenuItem *item, *ItemAddress();
  217.     while (menunum != MENUNULL)
  218.         {
  219.         switch(ITEMNUM(menunum))
  220.             {
  221.             case 0:
  222.                 read_file();
  223.                   clr_grf();
  224.                 break;
  225.             case 1:
  226.                 draw_graph();
  227.                 break;
  228.             case 2:
  229.                 do_avgs();
  230.                 break;
  231.             case 3:
  232.                 get_opt();
  233.                 break;
  234.             case 4:
  235.                 do_print();
  236.                 break;
  237.             case 5:
  238.                 do_help();
  239.                 break;
  240.             case 6:
  241.                 done(0);
  242.                 break;
  243.             }
  244.         item = ItemAddress(&MenuList1,menunum);
  245.         menunum = item->NextSelect;
  246.         }
  247. }
  248.  
  249.  
  250. /*
  251.  * do_print : dump the window to the printer
  252.  */
  253.  
  254. union printerIO {
  255.     struct IOStdReq ios;
  256.     struct IODRPReq iodrp;
  257.     struct IOPrtCmdReq iopc;
  258.     };
  259.  
  260. extern union printerIO *CreateExtIO();
  261. extern struct MsgPort *CreatePort();
  262.  
  263. do_print()
  264. {
  265. union printerIO *request;
  266. struct MsgPort *printerPort;
  267. struct ViewPort *vp;
  268. struct Screen *sc;
  269.  
  270.     ClearMenuStrip(Wind);
  271.     SetWaitPointer(Wind);
  272.  
  273.     /* set up for dump rastport request */
  274.     printerPort = CreatePort("myprport",0L);
  275.     request = CreateExtIO(printerPort, sizeof(union printerIO));
  276.     if (OpenDevice("printer.device",0L,request,0L) !=0)
  277.         {
  278.         AutoRequest(Wind,&prfailtxt,0L,&oktxt,0L,0L,300L,75L);
  279.         goto cleanup;
  280.         }
  281.  
  282.     request->iodrp.io_Command = PRD_DUMPRPORT;
  283.     request->iodrp.io_RastPort = rp;
  284.     sc = Wind->WScreen;
  285.     vp = &sc->ViewPort;
  286.     request->iodrp.io_ColorMap = vp->ColorMap;
  287.     request->iodrp.io_Modes = vp->Modes;
  288.     request->iodrp.io_RastPort = rp;
  289.     request->iodrp.io_SrcX=2L;
  290.     request->iodrp.io_SrcY=11L;
  291.     request->iodrp.io_SrcWidth=635L;
  292.     request->iodrp.io_SrcHeight=187L;
  293.     request->iodrp.io_DestCols=0L;
  294.     request->iodrp.io_DestRows=0L;
  295.     request->iodrp.io_Special=SPECIAL_ASPECT | SPECIAL_FULLCOLS;
  296.     DoIO(request);
  297.     CloseDevice(request);
  298.   cleanup:
  299.     DeleteExtIO(request, sizeof(union printerIO));
  300.     DeletePort(printerPort);
  301.     SetMenuStrip(Wind,&MenuList1);
  302.     ClearPointer(Wind);
  303. }
  304.  
  305. /*
  306.  * do_intro : display sign on text
  307.  */
  308.  
  309. do_intro()
  310. {
  311.     WindowToFront(Wind);
  312.     clr_grf();
  313.     PrintIText(rp,&IntroText1,0L,0L);    /* Print the text */
  314. }
  315.  
  316. /*
  317.  * do_avgs : display averages and min/max readings foe current data
  318.  */
  319.  
  320.  
  321. do_avgs()
  322. {
  323.    float firstod = 0, miles, od;
  324.    float lastod, prevod;
  325.    float gallons, cost, price;
  326.    float totgal = 0, totcost = 0;
  327.    int count = 0;
  328.    int year, i;
  329.  
  330.    float maxtank = 0;
  331.    float maxcost = 0;
  332.    float maxpr = 0;
  333.    float maxgal = 0;
  334.    float minpr =1e10;
  335.    int maxtyr;
  336.    int maxcostyr;
  337.    int maxpryr;
  338.    int maxgalyr;
  339.    int minpryr;
  340.  
  341.    SetWaitPointer(Wind);
  342.  
  343.    for (i=0; i<numrecs; i++)
  344.       {
  345.  
  346.       year = yrsave[i];
  347.  
  348.       /* eliminate undesired years */
  349.  
  350.       if (eyr != -1)    
  351.           {
  352.         if (year > eyr) break;
  353.         }
  354.  
  355.       if (syr != -1)    
  356.           {
  357.         if (year < syr) continue;
  358.         }
  359.  
  360.  
  361.       od = odsave[i];    /* get odometer reading */
  362.  
  363.       if (!firstod) firstod = od;
  364.       else {
  365.           float t = od-prevod;
  366.         if (t > maxtank) 
  367.             {
  368.             maxtank = t;
  369.             maxtyr = year;
  370.             }
  371.         }
  372.  
  373.       lastod = od;
  374.  
  375.  
  376.       price = pricesave[i];        /* get price */
  377.  
  378.       cost = rawcost[i];        /* get cost */
  379.  
  380.       gallons = cost/price;
  381.       /* accumulate data */
  382.       if (count != 0)
  383.           {
  384.          totgal += gallons;
  385.          totcost += cost;
  386.         }
  387.  
  388.       if (price > maxpr) { maxpr = price; maxpryr = year; }
  389.       if (price < minpr) { minpr = price; minpryr = year; }
  390.       if (gallons > maxgal) { maxgal = gallons; maxgalyr = year; }
  391.       if (cost > maxcost) { maxcost = cost; maxcostyr = year; }
  392.  
  393.       count++;
  394.       prevod = od;
  395.       }
  396.  
  397.       if (count < 2)
  398.           {
  399.         AutoRequest(Wind,&nodatamsg,0L,&oktxt,0L,0L,300L,75L);
  400.         return;
  401.         }
  402.  
  403.       /* print results */
  404.       miles = lastod-firstod;
  405.  
  406.       clr_grf();
  407.       sprintf(tempbuf,"%d transactions processed", count);
  408.       Move(rp,50L,50L);
  409.       Text(rp,tempbuf,strlen(tempbuf));
  410.       sprintf(tempbuf,"Maximum distance between transactions= %-5.1f miles in %d",
  411.             maxtank, maxtyr);
  412.       Move(rp,50L,60L);
  413.       Text(rp,tempbuf,strlen(tempbuf));
  414.       sprintf(tempbuf,"Maximum gallons = %-5.1f in %d", maxgal, maxgalyr);
  415.       Move(rp,50L,70L);
  416.       Text(rp,tempbuf,strlen(tempbuf));
  417.       sprintf(tempbuf,"Maximum cost = $%-6.2f in %d", maxcost, maxcostyr);
  418.       Move(rp,50L,80L);
  419.       Text(rp,tempbuf,strlen(tempbuf));
  420.       sprintf(tempbuf,"Maximum gasoline price = $%-6.3f in %d", maxpr, maxpryr);
  421.       Move(rp,50L,90L);
  422.       Text(rp,tempbuf,strlen(tempbuf));
  423.       sprintf(tempbuf,"Minimum gasoline price = $%-6.3f in %d", minpr, minpryr);
  424.       Move(rp,50L,100L);
  425.       Text(rp,tempbuf,strlen(tempbuf));
  426.       sprintf(tempbuf,"Total Miles = %-8.1f", miles);
  427.       Move(rp,50L,110L);
  428.       Text(rp,tempbuf,strlen(tempbuf));
  429.       sprintf(tempbuf,"Total Gallons = %-7.1f", totgal);
  430.       Move(rp,50L,120L);
  431.       Text(rp,tempbuf,strlen(tempbuf));
  432.       sprintf(tempbuf,"Total Cost = $%-8.2f", totcost);
  433.       Move(rp,50L,130L);
  434.       Text(rp,tempbuf,strlen(tempbuf));
  435.       sprintf(tempbuf,"Average Cost per Gallon = $%-6.4f", totcost/totgal);
  436.       Move(rp,50L,140L);
  437.       Text(rp,tempbuf,strlen(tempbuf));
  438.       sprintf(tempbuf,"Average Miles per Gallon = %-5.2f", miles/totgal);
  439.       Move(rp,50L,150L);
  440.       Text(rp,tempbuf,strlen(tempbuf));
  441.       sprintf(tempbuf,"Average Cost per Mile = %-5.2f cents", totcost/miles*100);
  442.       Move(rp,50L,160L);
  443.       Text(rp,tempbuf,strlen(tempbuf));
  444.       ClearPointer(Wind);
  445. }
  446.  
  447. /*
  448.  * done - just clean up that which is open, and then leave.
  449.  */
  450. done(how)
  451. int how;
  452.     {
  453.     if (Wind) 
  454.         {
  455.         ClearMenuStrip(Wind);
  456.         CloseWindow(Wind) ;
  457.         }
  458.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  459.     if (GfxBase) CloseLibrary(GfxBase) ;
  460.     if (DosBase) CloseLibrary(DosBase);
  461.     if (FIOSupp) ReleaseFileIO(FIOSupp);
  462.  
  463.     /* OpenWorkBench() ;        /* As requested */
  464.     exit(how) ;
  465.     }
  466.  
  467.  
  468.  
  469.  
  470. /*
  471.  * clr_grf : clear the graphics area of the screen
  472.  *
  473.  */
  474. clr_grf()
  475. {
  476.     SetAPen(rp,0L);
  477.     RectFill(rp,2L,11L,637L,198L);
  478.     SetAPen(rp,3L);
  479. }
  480.  
  481. /*
  482.  * do_help : display help text in main window
  483.  *
  484.  */
  485.  
  486. do_help()
  487. {
  488.     WindowToFront(Wind);
  489.     clr_grf();
  490.     PrintIText(rp,&HelpText2,0L,0L);    /* Print help text */
  491.  
  492. }
  493. /*
  494.  * abort handler : call by manx Chk_Abort() routine
  495.  */
  496.  
  497. _abort()
  498. {
  499.     printf("^C\n");
  500.     done(20);
  501. }
  502.